package com.handyshop.mall.user.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.handyshop.mall.dubbo.user.entity.SysPlatform;
import com.handyshop.mall.dubbo.user.entity.SysPlatformMenu;
import com.handyshop.mall.user.mapper.SysPlatformMapper;
import com.handyshop.mall.user.service.SysMenuService;
import com.handyshop.mall.user.service.SysPlatformMenuService;
import com.handyshop.mall.user.service.SysPlatformService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hs
 */
@Service
@RequiredArgsConstructor
public class SysPlatformServiceImpl extends ServiceImpl<SysPlatformMapper, SysPlatform> implements SysPlatformService {
    private final SysPlatformMenuService sysPlatformMenuService;
    private final SysMenuService sysMenuService;

    /**
     * 通过ID查询相关信息
     *
     * @param id id
     * @return 信息
     */
    @Override
    public SysPlatform findById(Long id) {
        SysPlatform sysPlatform = this.getById(id);
        if (sysPlatform == null) {
            return null;
        }
        LambdaQueryWrapper<SysPlatformMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysPlatformMenu::getPlatformId, sysPlatform.getId());
        List<SysPlatformMenu> sysPlatformMenus = sysPlatformMenuService.list(wrapper);
        if (CollUtil.isEmpty(sysPlatformMenus)) {
            return sysPlatform;
        }
        List<Long> menuIds = sysPlatformMenus.stream().map(SysPlatformMenu::getMenuId).collect(Collectors.toList());
        sysPlatform.setMenuTree(sysMenuService.getMenuTree(menuIds));
        return sysPlatform;
    }
}
